$(document).ready(function () {
    var currentUrl = window.location.href;
    // 打开弹窗
    $(".set_cargo_masking").click(function () {
        $(".modal-overlay").addClass("active");
        $(".modal-overlay .modal-content>div").hide();
        $(".modal-overlay .modal-content .set_cargo").show();
    });

    // 关闭弹窗 - 通过关闭按钮
    $(".close-btn").click(function () {
        $(".modal-overlay").removeClass("active");
    });

    // 关闭弹窗 - 通过取消按钮
    $(".btn-set-cargo-close").click(function () {
        $(".modal-overlay").removeClass("active");
    });

    // 关闭弹窗 - 通过确定按钮
    $(".btn-set-cargo-sure").click(function () {
        // 这里可以添加确定按钮的逻辑
        $(".modal-overlay").removeClass("active");
    });

    // 点击蒙层区域关闭弹窗
    $(".modal-overlay").click(function (event) {
        // 检查点击的是否是蒙层本身，而不是弹窗内容
        if ($(event.target).closest(".modal").length === 0) {
            $(".modal-overlay").removeClass("active");
        }
    });

    // 点击创建装车单弹窗
    $(".get_logistics_order_masking").on("click", function () {
        // 列表栏 -- 移除除第一行外的所有行
        $(".modal-body .logistics_info_table .logistics_info_table_row:gt(0)").remove();
        // 选择下拉框 -- 移除所有数据
        $(".modal-overlay .modal-content .create_transport_order .modal-body .logistics_plate_number #cars option").remove();
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".modal-overlay").addClass("active");
                $(".modal-overlay .modal-content>div").hide();
                $(".modal-overlay .modal-content .create_transport_order").show();
            },
            success_call_back: function (content) {

                // 列表栏 --- 填充数据
                $.each(content.cargo_list, function (index, cargo) {
                    console.log("填充数据:" + index);
                    var cargoItem = $(".modal-overlay .modal-content .create_transport_order .modal-body .logistics_info_table");
                    var newRowHtml = `
                        <div class="logistics_info_table_row">
                            <div class="logistics_info_table_col">${cargo.code}</div>
                            <div class="logistics_info_table_col">${cargo.name}</div>
                            <div class="logistics_info_table_col">${cargo.spec}</div>
                            <div class="logistics_info_table_col">${cargo.surplus}</div>
                            <div class="logistics_info_table_col"><input type="number" value="0"></div>
                        </div>
                    `;
                    cargoItem.append(newRowHtml);
                })

                $.each(content.plate_number_list, function (index, plate_number) {
                    var plate_number_select = $(".modal-overlay .modal-content .create_transport_order .modal-body .logistics_plate_number #cars");
                    console.log(plate_number_select)
                    plate_number_select.append('<option value="' + plate_number + '">' + plate_number + '</option>');
                })

            },
            error_call_back: function (content) {
                alert("获取装车单详情异常");
            }
        })

    })

    // 点击获取装车单列表弹窗
    $(".get_logistics_transport_order_masking").on("click", function () {
        // 列表栏 -- 移除除第一行外的所有行
        $(".modal-body .logistics_transport_table .logistics_transport_table_row:gt(0)").remove();
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".modal-overlay").addClass("active");
                $(".modal-overlay .modal-content>div").hide();
                $(".modal-overlay .modal-content .get_logistics_transport").show();
            },
            success_call_back: function (content) {
                if (content.code == 0) {
                    var cargoItem = $(".modal-overlay .modal-content .get_logistics_transport .modal-body .logistics_transport_table");
                    var newRowHtml = `
                        <div class="logistics_transport_table_row">
                            <div class="logistics_transport_table_col">未创建装车单</div>
                        </div>
                    `;
                    cargoItem.append(newRowHtml);
                } else {
                    // 列表栏 --- 填充数据
                    $.each(content.transport_list, function (index, transport) {
                        console.log("填充数据:" + index);
                        var cargoItem = $(".modal-overlay .modal-content .get_logistics_transport .modal-body .logistics_transport_table");
                        var newRowHtml = `
                        <div class="logistics_transport_table_row">
                            <div class="logistics_transport_table_col">${transport.transport_no}</div>
                            <div class="logistics_transport_table_col">${transport.status}</div>
                            <div class="logistics_transport_table_col">${transport.create_time}</div>
                        </div>
                    `;
                        cargoItem.append(newRowHtml);
                    })

                }

            },
            error_call_back: function (content) {
                alert("获取装车单详情异常");
            }
        })

    })


});